﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;

namespace RPGMapRevealer
{
    /// <summary>
    /// Common utility functions not associated with any particular class or object
    /// </summary>
    internal static class Util
    {
        /// <summary>
        /// Flattens a collection of collection of objects into a list of objects
        /// </summary>
        /// <typeparam name="T">The type of the objects</typeparam>
        /// <param name="steppedResults">the collection of collection of objects</param>
        /// <returns>Returns a single list of all the objects</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public static IList<T> Flatten<T>(this IEnumerable<IEnumerable<T>> steppedResults)
        {
            Contract.Requires(steppedResults != null);
            List<T> results = new List<T>();
            foreach (IEnumerable<T> step in steppedResults)
            {
                if (step != null)
                {
                    results.AddRange(step);
                }
            }
            return results;
        }

        public static int Abs(int value)
        {
            Contract.Ensures(Contract.Result<int>() >= 0);
            return (value == int.MinValue) ? int.MaxValue : Math.Abs(value);
        }
    }
}
